<?php

namespace traits\controller;

trait QueryTraits
{

    /**
     * 获取通用搜索器数据
     * @return array
     */
    protected function getSearchData(){
        request()->filter(['strip_tags']);
        $searchField = request()->param('search_field/s');
        $searchValue = request()->param('search_value/s');
        $filter      = request()->param('filter', '', null);
        return [
            'filter'       => $filter,
            'search_field' => $searchField,
            'search_value' => $searchValue,
        ];
    }

    /**
     * 组装selectPage查询条件
     * @return array
     */
    protected function getSelectPage()
    {
        request()->filter(['strip_tags']);
        //处理排序
        $orderBy = (array)request()->request("orderBy/a");
        $order   = [];
        foreach ($orderBy as $k => $v) {
            $order[$v[0]] = $v[1];
        }
        //处理默认选中
        $searchTable = request()->param('searchTable'); //查询字段前缀
        $searchKey   = request()->param('searchKey'); //查询初始化数据
        $searchValue = request()->param('searchValue'); //查询初始化数据
        $query       = [];
        if (!empty($searchKey) && $searchValue!='') {
            if ($searchTable != 'tbl') $searchKey = $searchTable . '.' . $searchKey;
            $query[] = [$searchKey, 'in', $searchValue];
        }
        //字段搜索
        $word        = (array)request()->param("q_word/a"); //搜索关键词,客户端输入以空格分开,这里接收为数组
        $searchField = request()->request("searchField"); //搜索字段逗号隔开
        if (!empty($searchField) && !empty($word)) {
            foreach ($word as $k => $v) {
                if (!empty($v)) {
                    $query[] = [$searchField, "like", "%{$v}%"];
                }
            }
        }
        return [
            'order' => $order,
            'query' => $query
        ];
    }

}